package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import pojo.Customer;
import service.CustomerService;
import test.MD5Tool;
import util.DataResponse;

/**
 * Servlet implementation class CustomerServlet
 */
@WebServlet("/CustomerServlet")
public class CustomerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CustomerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if(method.equals("login")) {
			login(request,response);
		}else if(method.equals("reg")) {
			reg(request,response);
		}
	}
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String name=request.getParameter("c_name");
		String pwd=request.getParameter("c_pass");
		Customer c = new Customer();
		c.setC_name(name);
		c.setC_pass(pwd);
		CustomerService cservice = new CustomerService();
		Customer cu=cservice.check_login(c);
		if(cu!=null) {
//			HttpSession session=request.getSession(true);
//			session.setAttribute("c",cu);
			DataResponse d = new DataResponse("200","success  小静同学",cu);//封装返回给前端的数据
			//把d转换成json字符串------》导入Gson.jar-->  产生json字符串的工具
			String json = new Gson().toJson(d);
			System.out.println(json);
			response.getWriter().write(json);
		}else {
			DataResponse d = new DataResponse("500","error 小静同学",cu);//封装返回给前端的数据
			String json = new Gson().toJson(d);
			System.out.println(json);
			response.getWriter().write(json);
			
		}
		}
	public void reg(HttpServletRequest request, HttpServletResponse response) {
		try {
			request.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name=request.getParameter("c_name");
		String pwd=MD5Tool.stringToMD5(request.getParameter("c_pass"));
		Customer c=new Customer();
		c.setC_name(name);
		c.setC_pass(pwd);
		CustomerService cservice=new CustomerService();
		int i=cservice.reg(name,pwd);
		if(i>0){
			try {
				response.sendRedirect("success.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				response.sendRedirect("fail.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
	}
}
